package cn.yanhu.kuwanapp.welfare.ad.csj

import android.app.Activity
import android.content.Context
import android.view.ViewGroup
import cn.yanhu.kuwanapp.utils.ext.logcom
import cn.yanhu.kuwanapp.utils.ext.showToast
import cn.yanhu.kuwanapp.welfare.ad.AdSlotType
import cn.yanhu.kuwanapp.welfare.ad.IAdProcess
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdNative.RewardVideoAdListener
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTRewardVideoAd


/**
 *穿山甲广告
 *@author woochen
 *@time 2020-01-04 18:22
 */
class CsjAdProcess : IAdProcess {

    override var mAdLoadLifecycle: IAdProcess.AdLoadLifecycle? = null


    override fun loadAd(context: Context,
                        slotType: Int,
                        codeId: String,
                        isPortation: Boolean,
                        innerCodeId: String,
                        containerView: ViewGroup?,
                        adTimeOut: Int) {

        when (slotType) {
            AdSlotType.AD_VIDEO -> {
                loadRewardVideo(context, codeId, isPortation, innerCodeId)
            }
        }

    }


    /**
     * 加载激励视频
     */
    private fun loadRewardVideo(context: Context,
                                codeId: String,
                                portation: Boolean,
                                innerCodeId: String) {
        val mTTAdNative = TTAdSdk.getAdManager().createAdNative(context)
        val adSlot = AdSlot.Builder().setCodeId(codeId).setExpressViewAcceptedSize(500f, 500f)
            .setOrientation(if (portation) TTAdConstant.HORIZONTAL else TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
            .build()
        mTTAdNative.loadRewardVideoAd(adSlot, object : RewardVideoAdListener {
            //请求广告失败
            override fun onError(code: Int, message: String?) {
                logcom("csj:onError code:${code} message:${message}")
                mAdLoadLifecycle?.adLoadFail("$code", "csj:${message}")
            }

            //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
            override fun onRewardVideoCached() {
                mIsLoaded = true
                showAd(context, AdSlotType.AD_VIDEO)
            }

            override fun onRewardVideoCached(p0: TTRewardVideoAd?) {

            }

            //视频广告的素材加载完毕，比如视频url等，在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
            override fun onRewardVideoAdLoad(ad: TTRewardVideoAd) {
                mIsLoaded = false
                mttRewardVideoAd = ad
                mttRewardVideoAd?.setRewardAdInteractionListener(object :
                    TTRewardVideoAd.RewardAdInteractionListener {
                    override fun onRewardVerify(rewardVerify: Boolean,
                                                rewardAmount: Int,
                                                rewardName: String?,
                                                code: Int,
                                                msg: String?) {
                        if (rewardVerify) { //有效播放
                            mAdLoadLifecycle?.adReward(innerCodeId)
                        }
                        logcom("csj:onRewardVerify:code->${code} msg->${msg} rewardVerify->${rewardVerify} rewardAmount->${rewardAmount} rewardName->${rewardName}")
                    }

                    override fun onSkippedVideo() {
                        logcom("csj:onSkippedVideo")
                    }

                    override fun onAdShow() {
                        logcom("csj:onAdShow")
                        mAdLoadLifecycle?.adPlayStart(innerCodeId)
                    }

                    override fun onAdVideoBarClick() {
                        logcom("csj:onAdVideoBarClick")
                        mAdLoadLifecycle?.adClicked(innerCodeId)
                    }

                    override fun onVideoComplete() {
                        logcom("csj:onVideoComplete")
                        mAdLoadLifecycle?.adPlayComplete(innerCodeId)
                    }

                    override fun onAdClose() {
                        logcom("csj:onAdClose")
                        mAdLoadLifecycle?.adDismissed(innerCodeId)
                    }

                    override fun onVideoError() {
                        logcom("csj:onVideoError")
                        mAdLoadLifecycle?.adLoadFail("10000", "csj:广告播放失败")
                    }
                })
            }
        })
    }

    private var mttRewardVideoAd: TTRewardVideoAd? = null
    private var mIsLoaded: Boolean = false

    /**
     * 资源回收
     */
    private fun releaseResource() {
        if (mttRewardVideoAd != null) {
            mttRewardVideoAd = null
        }
    }

    override fun showAd(context: Context, slotType: Int) {
        when (slotType) {
            AdSlotType.AD_VIDEO -> {
                if (mttRewardVideoAd != null && mIsLoaded && context is Activity) {
                    //展示广告，并传入广告展示的场景
                    mttRewardVideoAd?.showRewardVideoAd(context, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
                    mttRewardVideoAd = null
                } else {
                    showToast("请先加载广告")
                }
            }
        }
    }

    override fun release() {
        releaseResource()
    }
}